%%%-------------------------------------------------------------------
%%%    BASIC INFORMATION
%%%-------------------------------------------------------------------
%%% @copyright 2006 Erlang Training & Consulting Ltd
%%% @author  Martin Carlson <martin@erlang-consulting.com>
%%% @version 0.0.1
%%% @doc Interface module for the PostgreSQL driver
%%% @end
%%%-------------------------------------------------------------------
-module(psql).
-author("support@erlang-consulting.com").
-copyright("Erlang Training & Consulting Ltd").
-vsn("$Rev").

%% API
-export([q/2]).

-define(REREQUEST_FREQ, 1000).

%% Global types
%% @type query() = string(). 
%% SQL conformant query with omitted semicolon
%%
%% @type result() = [] | [Row::tuple()] | {sql_error, term()}. 
%% Query results, rows are represented as tuples and 
%% values are converted to the closest erlang type 

%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% @spec q(Pid::pid(), Query::query()) -> result() 
%% @doc Query the database, returns the empty list or a result set
%% @end
%%--------------------------------------------------------------------
q(Pid, Query) ->
    case psql_logic:q(Pid, Query) of
	{ok, {_Desc, Data}} ->
	    Data;
	{sql_error, Reason} ->
	    {sql_error, Reason};
	{error, _Reason} ->
	    timer:sleep(?REREQUEST_FREQ),
	    q(Pid, Query)
    end.

%%====================================================================
%% Internal functions
%%====================================================================
